﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using ErgoBreak.Core.Resources;
using ErgoBreak.Presentation.Components;

namespace ErgoBreak
{
    class Program : IDisposable
    {
        private Tray tray;
        private ResourceCollection resources;
        
        // Singletone instance
        private static Program program;

        public Program(ResourceCollection rcCollection, Tray trayComponent)
        {
            this.tray = trayComponent;
            this.resources = rcCollection;
        }

        public void Run()
        {
            Application.EnableVisualStyles();
            this.tray.Enabled = true;
            Application.Run();
        }

        public void Exit()
        {
            // clean up everything and exit the program
            this.Dispose();
            Application.Exit();
        }

        public void Dispose()
        {
            // release the memory and resources used
            this.resources.Dispose();
            this.tray.Dispose();                
        }

        public static void ExitApplication()
        {
            program.Exit();
        }

        [STAThread]
        public static void Main(string[] args)
        {
            bool isFirstInstance;
            using (System.Threading.Mutex mtx = new System.Threading.Mutex(true, "ErgoBreak", out isFirstInstance))
            {
                if (isFirstInstance)
                {
                    ProgramBuilder builder = new ProgramBuilder(Application.StartupPath);
                    Program.program = builder.Build();
                    program.Run();
                }
                else
                {
                    MessageBox.Show("Another instance of the application is already running.");
                }
            }
        }

    }
}
